package com.nulldev.util.web.HttpServer3.extensions.envStats;

import com.nulldev.util.languages.json.uniapi.UniJsonValue;
import com.nulldev.util.manage.nullUtil;
import com.nulldev.util.web.HttpServer3.Response;
import com.nulldev.util.web.HttpServer3.pages.v3.HttpPage3;

public class TCPDebugPage extends HttpPage3 {

	private static final String JSON_MIME = "application/json; charset=utf-8";

	public TCPDebugPage() {
		super("/debug/tcp");
	}

	@SuppressWarnings("resource")
	@Override
	public Response onRequest(final Request request) {
		if (!nullUtil.IS_NON_MAINLINE_BUILD)
			return new Response(204, BLANK_IO_STREAM);
		final UniJsonValue uv = UniJsonValue.object(6);
		uv.add("rcvBuf", request.socket_rcvbuf());
		uv.add("sndBuf", request.socket_sndbuf());
		uv.add("scheme", request.protocolScheme());
		uv.add("prot_def", request.protocolDefinition());
		if (request.protocolScheme().equalsIgnoreCase("https")) {
			uv.add("cipher", request.socket_tlsCipher());
			uv.add("version", request.socket_tlsVersion());
		}
		return new Response(200, uv, uv).setCustomMime(JSON_MIME);
	}

}
